//
//  BatchDaoViewController.swift
//  EnglishDictionary
//
//  Created by QFX on 2024/3/4.
//

import UIKit

class BatchDaoViewController: UIViewController {

    @IBOutlet weak var daoTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "一键导入数据"
        let barButtonItem1 = UIBarButtonItem(title: "导入", style: .plain, target: self, action: #selector(dao))
        self.navigationItem.rightBarButtonItem = barButtonItem1
        
        self.daoTextView.layer.cornerRadius = 10
        self.daoTextView.clipsToBounds = true
        self.daoTextView.layer.borderWidth = 1
        self.daoTextView.layer.borderColor = UIColor.black.cgColor
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
            self.daoTextView.becomeFirstResponder()
        }
        
        
    }
    
    @objc func dao(){
        if self.daoTextView.text.count == 0 {
            TRHUD.showSuccess("请黏贴你要导入的数据，注意格式")
            return
        }
        
        let trimmedStr = self.daoTextView.text.trimmingCharacters(in: .whitespaces)
        let lines = trimmedStr.components(separatedBy: "\n")
        var keys:[String] = []
        var values:[String] = []
        for (i,data) in lines.enumerated() {
            if i % 2 == 0{
                keys.append(data)
            }else{
                values.append(data)
            }
        }
        if lines.count == 0 {
            TRHUD.showSuccess("请注意格式每个导入数据独自一行")
            return
        }
        
        if keys.count != values.count {
            TRHUD.showSuccess("请检查导入的数据，是否缺少一个词汇、句子或者翻译")
            return
        }
        
        let actionSheet = VHLActionSheetView(title: "选择你要添加的类别", cancelButtonTitle: "取消", otherButtonTitles: ["Words","Sentences"]) { av, buttonType in
            if case let .other(index) = buttonType {
                 if index == 0 {
                     self.selectAddFold(cate: "Words",keys: keys,values: values)
                 } else if index == 1 {
                     self.selectAddFold(cate: "Sentences",keys: keys,values: values)
                 }
            }
        }
        actionSheet.show()
        
        print(keys)
        print(values)
    }

    func addSubFold(fold:String,cate:String,keys:[String],values:[String]){
        if cate == "Words"{
            for (i,key) in keys.enumerated() {
                let word = Word()
                word.word = key
                word.st = values[i]
                word.fold = fold
                try?word .saveOrUpdate()
            }
        }else if cate == "Sentences"{
            for (i,key) in keys.enumerated() {
                let sentence = Sentence()
                sentence.sentence = key
                sentence.st = values[i]
                sentence.fold = fold
                try?sentence .saveOrUpdate()
            }
        }
        TRHUD.showSuccess("导入成功")
        self.navigationController?.popViewController(animated: true)
    }
    

    func selectAddFold(cate:String,keys:[String],values:[String]){
        var foldx:[String] = []
        if  let folds = Fold.getFolds(parentFold: cate),folds.count > 0{
            for fold in folds {
                let foldName = fold.fold
                foldx.append(foldName!)
            }
        }

        let actionSheet = VHLActionSheetView(title: "选择你要添加的目录", cancelButtonTitle: "取消", otherButtonTitles: foldx) { av, buttonType in
            if case let .other(index) = buttonType {
                let foldName = foldx[index]
                var foldx1:[String] = []
                if let folds = Fold.getFolds(parentFold: foldName),folds.count > 0{
                    for fold in folds {
                        let foldName = fold.fold
                        foldx1.append(foldName!)
                    }
                    let actionSheet1 = VHLActionSheetView(title: "选择你要添加的目录", cancelButtonTitle: "取消", otherButtonTitles: foldx1) { av, buttonType in
                        if case let .other(index) = buttonType {
                            let foldName = foldx1[index]
                            self.addSubFold(fold: foldName, cate: cate,keys: keys,values: values)
                        }
                    }
                    actionSheet1.show()
                }else{
                    self.addSubFold(fold: foldName, cate: cate,keys: keys,values: values)
                }
            }
        }
        actionSheet.show()
    }

}
